package com.myProject.order.utils;

import com.myProject.order.entity.IdEntity;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.metamodel.SingularAttribute;
import java.lang.annotation.Annotation;
import java.util.UUID;

/**
 * Created by xhw on 2017/08/06.
 */
public class JPAUtils {
    public static String getEntityName(Class<? extends IdEntity> entityType)
    {
        try
        {
            Annotation a = entityType.getAnnotation(Entity.class);

            if (a != null) {
                String tableName = (String)a.getClass().getMethod("name", new Class[0]).invoke(a, new Object[0]);

                return StringUtils.isEmpty(tableName) ? entityType.getSimpleName() : tableName;
            }

            return entityType.getSimpleName();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getTableName(Class<? extends IdEntity> entityType)
    {
        try
        {
            Annotation a = entityType.getAnnotation(Table.class);

            if (a != null) {
                String tableName = (String)a.getClass().getMethod("name", new Class[0]).invoke(a, new Object[0]);

                String schemaName = (String)a.getClass().getMethod("schema", new Class[0]).invoke(a, new Object[0]);

                return new StringBuilder().append(StringUtils.isEmpty(schemaName) ? "" : new StringBuilder().append(schemaName).append(".").toString()).append(StringUtils.isEmpty(tableName) ? entityType.getSimpleName().toUpperCase() : tableName).toString();
            }

            return entityType.getSimpleName().toUpperCase();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isIdEntityField(Class<? extends IdEntity> entityType, String key)
    {
        try
        {
            java.lang.reflect.Field f = entityType.getDeclaredField(key);

            if (IdEntity.isIdEntityType(f.getType()))
                return true;
        } catch (SecurityException e) {
        }
        catch (NoSuchFieldException e) {
        }
        return false;
    }

    public static boolean isField(Class<? extends IdEntity> entityType, String key)
    {
        boolean result = Boolean.FALSE.booleanValue();
        try {
            java.lang.reflect.Field f = entityType.getDeclaredField(key);
            if (f != null)
                result = true;
        }
        catch (Exception ex) {
            result = false;
        }
        return result;
    }

    public static String newGUID() {
        return UUID.randomUUID().toString().toUpperCase();
    }

    public static String joinProperties(SingularAttribute[] attributes) {
        String[] properties = new String[attributes.length];
        for (int i = 0; i < attributes.length; i++) {
            properties[i] = attributes[i].getName();
        }
        return joinProperties(properties);
    }

    public static String joinProperties(String[] properties) {
        return StringUtils.join(properties, ".");
    }
}
